use Dyadic_OSV.dta, clear

global x1 rebel_strength c.l_lnbd i.l_pgm c.igo_score c.ICCPR c.ln_gdppc c.ln_pop 
global x2 rebel_strength c.l_lnbd i.l_pgm c.igo_score c.ICCPR c.ln_gdppc c.ln_pop c.efindex i.conflict_type c.lnaidall

***********
**Table 1**
***********

qui: logit OSV_gov_bi c.l_f_mil c.lpolity $x1, cluster(ccode) 
eststo clear
estpost summarize OSV_gov_bi PTS_A PTS_S latentmean ethnic_bi ethnic_bi2 nonethnic_bi nonethnic_bi2 l_f_mil l_f_icrg ln_milex ln_milper lpolity rebel_strength l_lnbd l_pgm igo_score ICCPR ln_gdppc ln_pop lnaidall efindex if e(sample)
esttab using table1.csv, replace cells("count(fmt(0)) mean(fmt(3)) sd(fmt(3)) min(fmt(0)) max(fmt(0))" ) noobs 

***********
**Table 2**
***********
qui: logit OSV_gov_bi c.l_f_mil c.lpolity $x1, cluster(ccode) 
eststo clear
estpost tab conflict_type if e(sample)
esttab using table2.csv, replace cells(" pct(fmt(2)) b(fmt(0))" ) noobs 

*****************************
*******Main Analysis ********
*****************************

***********
**Table 3**
***********
xtset dyad_id year
eststo clear
eststo m1: logit OSV_gov_bi c.l_f_mil c.lpolity $x1, cluster(ccode) 
eststo m2: logit OSV_gov_bi c.l_f_mil##c.lpolity $x1 i.year i.region, cluster(ccode)
eststo m3: nbreg OSV_gov c.l_f_mil##c.lpolity $x1 i.year i.region, cluster(ccode)  
eststo m4: logit OSV_gov_bi c.l_f_mil##c.lpolity $x2 i.year i.region, cluster(ccode) 
estout *, cells(b(star fmt(3)) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) stats(N, fmt(%9.0g %9.3f)) 
esttab using table3.csv, replace cells(b(star fmt(3)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) stats(aic N, fmt(%9.3f))

***********
**Table 4**
***********
xtset dyad_id year
eststo clear
eststo m1: logit OSV_gov_bi c.l_f_mil##c.ethnic_exclusion c.lpolity $x2 i.year i.region, cluster(ccode)
eststo m2: logit OSV_gov_bi c.l_f_mil##i.ethnic_exclusion_bi c.lpolity $x2 i.year i.region, cluster(ccode)
estout *, cells(b(star fmt(3)) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) stats(N, fmt(%9.0g %9.3f)) 
esttab using table4.csv, replace cells(b(star fmt(3)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) stats(N, fmt(%9.3f)) drop(*.year *.region) aic

***********
**Table 5**
***********
xtset dyad_id year
eststo clear
eststo m1: logit ethnic_bi c.l_f_mil c.lpolity $x2 i.year i.region, cluster(ccode)
eststo m2: logit ethnic_bi2 c.l_f_mil c.lpolity $x2 i.year i.region, cluster(ccode)
eststo m3: logit nonethnic_bi c.l_f_mil c.lpolity $x2 i.year i.region, cluster(ccode)  
eststo m4: logit nonethnic_bi2 c.l_f_mil c.lpolity $x2 i.year i.region, cluster(ccode)
estout *, cells(b(star fmt(3)) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) stats(N, fmt(%9.0g %9.3f)) 
esttab using table5.csv, replace cells(b(star fmt(3)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01)

************
**Figure 1**
************
qui: logit OSV_gov_bi c.l_f_mil##c.lpolity $x1 i.region i.year, cluster(ccode) 
margins, at(l_f_mil=(0(0.1)1) lpolity=(0 1))
marginsplot, title("(a) Predicted probabilities") xtitle("Military capacity") ytitle("Pr(government one-sided violence)") legend(position(6) rows(1) order(1 "Polity = 0" 2 "Polity = 1"))
margins, dydx(l_f_mil) at(lpolity=(0(0.1)1))
marginsplot, title("(b) Marginal effect of military capacity") xtitle("Polity score") ytitle("Marginal effect of military capacity") 

************
**Figure 2**
************
qui: logit ethnic_bi2 c.l_f_mil c.lpolity $x1 i.year i.region, cluster(ccode) 
margins, at(l_f_mil=(0(0.1)1))
marginsplot, title("Ethnically-targeted Government One-sided Violence") xtitle("Military capacity") ytitle("Pr(government one-sided violence)") 
qui: logit nonethnic_bi2 c.l_f_mil c.lpolity $x1 i.year i.region, cluster(ccode) 
margins, at(l_f_mil=(0(0.1)1)) 
marginsplot, title("Untargeted Government One-sided Violence") xtitle("Military capacity") ytitle("Pr(government one-sided violence)") 

********************************
*******Robustness checks********
********************************

***********
**Table 6**
***********
xtset dyad_id year
eststo clear
eststo m1: reg physint c.l_f_mil##c.lpolity $x2 i.region i.year, cluster(ccode)
eststo m2: reg disap c.l_f_mil##c.lpolity $x2 i.region i.year, cluster(ccode)
eststo m3: reg kill c.l_f_mil##c.lpolity $x2 i.region i.year, cluster(ccode)
eststo m4: reg polpris c.l_f_mil##c.lpolity $x2 i.region i.year, cluster(ccode)
eststo m5: reg tort c.l_f_mil##c.lpolity $x2 i.region i.year, cluster(ccode)
estout *, cells(b(star fmt(3)) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) stats(N, fmt(%9.0g %9.3f)) 
esttab using table6.csv, replace cells(b(star fmt(3)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) stats(r2 N, fmt(%9.3f)) drop(*.year *.region) 

************
**Table A1**
************
xtset dyad_id year
eststo clear
eststo m1: reg PTS_A c.l_f_mil##c.lpolity $x1 i.region i.year, cluster(ccode) 
eststo m2: reg PTS_S c.l_f_mil##c.lpolity $x1 i.region i.year, cluster(ccode)
eststo m3: reg latentmean c.l_f_mil##c.lpolity $x1 i.region i.year, cluster(ccode)
eststo m4: reg PTS_A c.l_f_mil##c.lpolity $x2 i.region i.year, cluster(ccode) 
eststo m5: reg PTS_S c.l_f_mil##c.lpolity $x2 i.region i.year, cluster(ccode)
eststo m6: reg latentmean c.l_f_mil##c.lpolity $x2 i.region i.year, cluster(ccode)
estout *, cells(b(star fmt(3)) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) stats(N, fmt(%9.0g %9.3f)) 
esttab using tableA1.csv, replace cells(b(star fmt(3)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) stats(N, fmt(%9.3f)) drop(*.year *.region)

************
**Table A2**
************
xtset dyad_id year
eststo clear
eststo m1: logit OSV_gov_bi c.ln_milex##c.lpolity $x1 i.year i.region, cluster(ccode) 
eststo m2: logit OSV_gov_bi c.ln_milex##c.lpolity $x2 i.year i.region, cluster(ccode)
eststo m3: logit OSV_gov_bi c.ln_milper##c.lpolity $x1 i.year i.region, cluster(ccode) 
eststo m4: logit OSV_gov_bi c.ln_milper##c.lpolity $x2 i.year i.region, cluster(ccode)
eststo m5: logit OSV_gov_bi c.l_f_icrg##c.lpolity $x1 i.year i.region, cluster(ccode) 
eststo m6: logit OSV_gov_bi c.l_f_icrg##c.lpolity $x2 i.year i.region, cluster(ccode)
estout *, cells(b(star fmt(3)) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) stats(N, fmt(%9.0g %9.3f)) 
esttab using tableA2.csv, replace cells(b(star fmt(3)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) stats(N, fmt(%9.3f)) drop(*.year *.region) 

************
**Table A3**
************
xtset dyad_id year
eststo clear
eststo m1: logit ethnic_bi2 c.ln_milex c.lpolity $x2 i.year i.region, cluster(ccode) 
eststo m2: logit nonethnic_bi2 c.ln_milex c.lpolity $x2 i.year i.region, cluster(ccode)
eststo m3: logit ethnic_bi2 c.ln_milper c.lpolity $x2 i.year i.region, cluster(ccode)
eststo m4: logit nonethnic_bi2 c.ln_milper c.lpolity $x2 i.year i.region, cluster(ccode) 
eststo m5: logit ethnic_bi2 c.l_f_icrg c.lpolity $x2 i.year i.region, cluster(ccode)
eststo m6: logit nonethnic_bi2 c.l_f_icrg c.lpolity $x2 i.year i.region, cluster(ccode)
estout *, cells(b(star fmt(3)) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.01) stats(N, fmt(%9.0g %9.3f)) 
esttab using tableA3.csv, replace cells(b(star fmt(3)) se(par fmt(3))) star(* 0.10 ** 0.05 *** 0.01) stats(N, fmt(%9.3f)) drop(*.year *.region) 
